package com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisposal.repository.impl;

import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisposal.repository.EmergencyDisposalRepository;
import com.css.fxfzypg.modules.suggestinfo.analysis.emergencydisposal.repository.entity.EmergencyDisposalEntity;
import com.css.fxfzypg.modules.suggestinfo.analysis.suggestionanalysis.vo.ParamsVo;
import com.css.fxfzypg.util.PlatformObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class EmergencyDisposalRepositoryImpl implements EmergencyDisposalRepository {
    @Autowired
    JdbcTemplate jdbcTemplate;
    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Boolean save(EmergencyDisposalEntity emergencyDisposalEntity) {
        String sql = "insert into ypg_emergency_disposal values(:id,:taskNum,:province,:provinceId,:city,:cityId,:area,:areaId," +
                ":rescueForceMaterialInput,:earthquakeDisasterCriticalArea,:trafficSystemSupportRepair,:communicationsSystemSupportRepair," +
                ":powerSystemSupportRepair,:importantFacilitiesCheck,:climateFeatureReply,:nationFeatureReply,:rumorReply,:otherFeatureReply," +
                ":divisionFlag,:delFlag,:createName,:createId,:createTime,:maintainer,:maintainerId,:maintainTime)";
        SqlParameterSource source = new BeanPropertySqlParameterSource(emergencyDisposalEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public Boolean update(EmergencyDisposalEntity emergencyDisposalEntity) {
        String sql = "update ypg_emergency_disposal set " +
                "rescue_force_material_input=:rescueForceMaterialInput,earthquake_disaster_critical_area=:earthquakeDisasterCriticalArea,traffic_system_support_repair=:trafficSystemSupportRepair,communications_system_support_repair=:communicationsSystemSupportRepair," +
                "power_system_support_repair=:powerSystemSupportRepair,important_facilities_check=:importantFacilitiesCheck,climate_feature_reply=:climateFeatureReply,nation_feature_reply=:nationFeatureReply,rumor_reply=:rumorReply,other_feature_reply=:otherFeatureReply," +
                "maintainer=:maintainer,maintainer_id=:maintainerId,maintain_time=:maintainTime where id = '"+emergencyDisposalEntity.getId()+"'";
        SqlParameterSource source = new BeanPropertySqlParameterSource(emergencyDisposalEntity);
        return namedParameterJdbcTemplate.update(sql, source) > 0;
    }

    @Override
    public EmergencyDisposalEntity getEmergencyDisposalData(ParamsVo params) {
        try{
            StringBuilder sql = new StringBuilder("select * from ypg_emergency_disposal ");
            StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+params.getTaskNum()+"' ");
            if(!PlatformObjectUtils.isEmpty(params.getProvinceId())){
                whereSql.append(" and province_id = '"+ params.getProvinceId()+"'");
            }
            if(!PlatformObjectUtils.isEmpty(params.getCityId())){
                whereSql.append(" and city_id = '"+ params.getCityId()+"'");
            }

            String flag = params.getFlag();
            if(flag.equals("1")){
                whereSql.append(" and division_flag = '1'");
            }else if (flag.equals("0")){
                if(!PlatformObjectUtils.isEmpty(params.getAreaId())){
                    whereSql.append(" and area_id = '"+ params.getAreaId()+"'");
                }
                whereSql.append(" and division_flag = '0'");
            }
            EmergencyDisposalEntity emergencyDisposalEntity = jdbcTemplate.queryForObject(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisposalEntity.class));
            return emergencyDisposalEntity;
        }catch (Exception e){
            return null;
        }
    }

    @Override
    public List<EmergencyDisposalEntity> getEmergencyDisposalDataByTaskNum(String taskNum) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disposal ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+taskNum+"'");
        List<EmergencyDisposalEntity> emergencyDisposalEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisposalEntity.class));
        return emergencyDisposalEntities;
    }

    @Override
    public EmergencyDisposalEntity getEmergencyDisposalDataByTaskNum(String taskNum, String cityId, String areaId) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disposal ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and task_num = '"+taskNum+"' and city_id = '"+cityId+"'  and area_id = '"+areaId+"' and division_flag = '0'");
        try {
            EmergencyDisposalEntity emergencyDisposalEntity = jdbcTemplate.queryForObject(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisposalEntity.class));
            return emergencyDisposalEntity;
        }catch (EmptyResultDataAccessException e){
            return null;
        }
    }

    @Override
    public int getCountById(String id) {
        int count = jdbcTemplate.queryForObject("select count(*) count from ypg_emergency_disposal where id = '"+id+"' ", Integer.class);
        return count;
    }

    @Override
    public List<EmergencyDisposalEntity> getEmergencyDisposalAreaData(ParamsVo paramsVo) {
        StringBuilder sql = new StringBuilder("select * from ypg_emergency_disposal ");
        StringBuilder whereSql = new StringBuilder("where del_flag = '0' and division_flag = '0' and task_num = '"+paramsVo.getTaskNum()+"' and province_id = '"+paramsVo.getProvinceId()+"' and city_id = '"+paramsVo.getCityId()+"'");
        List<EmergencyDisposalEntity> emergencyDisposalEntities = jdbcTemplate.query(sql.append(whereSql).toString(), new BeanPropertyRowMapper<>(EmergencyDisposalEntity.class));
        return emergencyDisposalEntities;
    }
}
